package com.zj.musicTools.page

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.insets.navigationBarsHeight
import com.zj.musicTools.R
import com.zj.musicTools.page.CourseTabs.LOG
import com.zj.musicTools.page.CourseTabs.MAIN
import com.zj.musicTools.page.CourseTabs.SET
import com.zj.musicTools.ui.theme.MusicToolsTheme
import com.zj.musicTools.ui.theme.Purple200
import com.zj.musicTools.ui.theme.musicWhite
import java.util.Locale


enum class CourseTabs(
    @StringRes val title:Int,
    @DrawableRes val icon:Int
){
    MAIN(R.string.home_page, R.drawable.ic_home_normal),
    LOG(R.string.log_page, R.drawable.ic_log_normal),
    SET(R.string.set_page, R.drawable.ic_set_normal)
}

/**
 * 标记函数，用于记录当前标签页
 * */
/*class MyViewModel : ViewModel(){
    private val _position = MutableLiveData(MAIN)
    val position:LiveData<CourseTabs> =_position

    fun onPositionChanged(newPosition: CourseTabs){
        _position.value = newPosition
    }

}*/


/**
 * 导航栏主函数，实现导航栏图标与页面函数之间的跳转
 *
 * https://www.cnblogs.com/mengdd/archive/2022/01/29/android-compose-window-insets-handling.html
 * https://blog.csdn.net/weixin_43948791/article/details/116935738
 *
 * */

//@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun NavigationPage(){
    val tabs=CourseTabs.values()
    val iconColor = Purple200
    var position by remember { mutableStateOf(MAIN) }

    Scaffold(
        containerColor = colorResource(id = R.color.white), //页面背景色
//        modifier = Modifier.padding(0.dp),
        bottomBar = {
            NavigationBar(
                Modifier
                    .navigationBarsHeight(additional = 50.dp),/*导航栏高度*/
                containerColor = colorResource(id = R.color.musicOrange),
            ){
//
                tabs.forEach {tab ->
                    NavigationBarItem(selected = tab == position,

                        icon = {
                            Icon(painterResource(id = tab.icon),
                                contentDescription = null,
                                Modifier
//                                    .padding(0.dp)
                                    .height(25.dp)
                                    .offset(y = (-2).dp),
                                tint=if(tab == position) iconColor else musicWhite)
                        },
                        label = {
                            Text(
                                stringResource(id = tab.title).uppercase(Locale.ROOT),
                                color=if(tab == position) iconColor else musicWhite,
                                modifier = Modifier
                                    .align(Alignment.Bottom)
                                    .offset(y = 19.dp),
                                fontSize = 11.sp
                            )
                        },
//                        modifier =  Modifier.fillMaxSize().background(colorResource(R.color.white)),
                        alwaysShowLabel = false,
                        onClick = { position=tab }

                        )
                }

//                }

            }
        }
    ) {
        innerPadding->Modifier.padding(innerPadding)
        when(position){
            MAIN -> MainPage()
            LOG -> logPage()
            SET -> setPage()
        }
    }
}
@Preview(showBackground = true)
@Composable
private fun DefaultPreview() {
    MusicToolsTheme {
        NavigationPage()
    }
}





